package com.budwk.app.sqfwDoc.controller;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfwDoc.dto.SqfwTemplateConfigPageQueryDTO;
import com.budwk.app.sqfwDoc.dto.SqfwTemplateDictConfigUpdateDTO;
import com.budwk.app.sqfwDoc.models.SqfwTemplateConfig;
import com.budwk.app.sqfwDoc.service.SqfwTemplateDicConfigService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import java.util.Map;


/**
 * @author ZY.Wang
 * @since 2023/12/5
 */
@IocBean
@At("/platform/sqfw/sqfwTemplateDictConfig")
@ApiDefinition(tag = "模板字段配置")
@Slf4j
public class SqfwTemplateDictConfigController {

    @Inject
    private SqfwTemplateDicConfigService sqfwTemplateDicConfigService;


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "查询模板字段配置")
    public Result<?> pageListConfig(@Param("..") SqfwTemplateConfigPageQueryDTO config) {
        Pagination pagination = sqfwTemplateDicConfigService.pageListConfig(config);
        return Result.data(pagination);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "添加模板字段配置")
    public Result<?> addDictConfig(@Param("..") SqfwTemplateDictConfigUpdateDTO data) {
        String userId = SecurityUtil.getUserId();
        String id = sqfwTemplateDicConfigService.addDictConfig(userId, data);
        return Result.data(id);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获模板字段配置详情")
    public Result<?> getDetail(@Param("id") String id) {
        SqfwTemplateConfig vo = sqfwTemplateDicConfigService.getDetail(id);
        return Result.success(vo);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "更新模板字段配置")
    public Result<?> updateDictConfig(@Param("..") SqfwTemplateDictConfigUpdateDTO data) {
        String userId = SecurityUtil.getUserId();
        sqfwTemplateDicConfigService.updateDictConfig(userId, data);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "上传模板文件并解析")
    @AdaptBy(type = UploadAdaptor.class)
    public Result<?> uploadTemplateFile(@Param("file") TempFile file, @Param("templateConfigId") String templateConfigId) {
        Map<String, String> fileUrlMap = sqfwTemplateDicConfigService.uploadTemplateFile(file, templateConfigId);
        return Result.data(fileUrlMap);
    }
}
